import {
	Button,
	Collapse,
	Form,
	Row,
	Col,
	Input,
	Select,
	DatePicker,
	Cascader,
	message,
	TreeSelect,
	InputNumber,
	Spin,
} from 'antd'
import React, {useState, useEffect, useRef} from 'react'
import {history, connect} from 'umi'
import moment from 'moment'
import {PlusCircleOutlined, MinusCircleOutlined} from '@ant-design/icons'
import ChannelSupplier from './components/ChannelSupplier'
import Assets from './components/Assets'
import Qualifications from './components/Qualifications'
import styles from './index.less'
import {
	addAssetSupplierAccess,
	addHandleSupplierAccess,
	addSPSupplierAccess,
	addSupplierAccess,
	checkAccessSocialRepeat,
	editAssetSupplierAccess,
	editCheckStatus,
	editHandleSupplierAccess,
	editSPSupplierAccess,
	editSupplierAccess,
	getCheckSocialRepeat,
	viewSupplierAccess,
} from '@/service/supplier/supplierAccess'
import {getDict} from '@/service/common/dict'
import Modal from '@/components/Modal'
import {getSupplierType, translateDataToTree} from '@/utils/common'
import {
	completeTask,
	deleteProcessInstance,
	getAllCitys,
	getAllCitysPermission,
	getCompany,
	getGenerateNumber,
	getSupAndSubCompany,
	processRecordSave,
} from '@/service/common'
import {getformDatas, getUrlData, getUrls} from './data'
import ManagementSupplier from './components/ManagementSupplier'
import {getPublicDownloadUrl} from '@/service/common/upload'
import Examine from '@/components/Examine'
import {getSubjectCompany} from '@/service/merchantCenter/clue'
import OperationRecord from '@/components/OperationRecord'

const {Panel} = Collapse
const {TextArea} = Input
const {Option} = Select

// 城市
const cityNames = {
	label: 'name',
	value: 'id',
	children: 'cityInfoList',
}

const fieldNames = {
	label: 'name',
	value: 'id',
	id: 'id',
	pId: 'parentId',
}

const newlyAdded = (props: any) => {
	const {query} = props.location
	const [type, setType] = useState<string>()
	const channelRef = useRef<any>(null)
	const managementRef = useRef<any>(null)
	const assetsRef = useRef<any>(null)
	const examineRef = useRef<any>(null)
	const [admittanceForm] = Form.useForm()
	const [basicsForm] = Form.useForm()
	const [bankForm] = Form.useForm()
	const [formData, setFormData] = useState<any>({})
	const [dictData, setDictData] = useState<any>({}) // 字典数据
	const [supplierType, setSupplierType] = useState<any[]>([]) // 选择的供应商类型
	const [supplierTypeData, setSupplierTypeData] =
		useState<supplierAPI.supplierType>([]) // 供应商类型数据
	const [isModalOpen, setIsModalOpen] = useState(false)
	const [isExamineModalOpen, setIsExamineModalOpen] = useState(false)
	const [required, setRequired] = useState(true) // 是否必填
	const [saveType, setSaveType] = useState(false)
	const [allCompanyData, setAllCompanyData] = useState([]) // 所有公司数据
	const [companyData, setCompanyData] = useState([]) // 所属公司数据
	const [dataSource, setDataSource] = useState<any>({})
	const [urlData, setUrlData] = useState<any>({})
	const [loading, setLoading] = useState(false)
	const [btnLoading, setBtnLoading] = useState(false)
	const [citysList, setCitysList] = useState([]) // 城市数据
	const [enclosureList, setEnclosureList] = useState<any[]>([])
	const [examineType, setExamineType] = useState<string>('')
	const [mainBodyData, setMainBodyData] = useState([]) // 主体数据
	const [mainBodyId, setMainBody] = useState() // 主体ID
	const [orgId, setOrgId] = useState<any>() // 公司ID

	/** 获取主体数据 */
	const getSubjectFun = () => {
		getSubjectCompany()
			.then((res: any) => {
				if (res.success) {
					setMainBodyData(res.data.data)
				}
			})
			.catch((err) => {
				message.error(err)
				setLoading(false)
			})
	}
	/** 获取城市列表 */
	const getCitysFun = () => {
		getAllCitys()
			.then((res: any) => {
				if (res?.success) {
					setCitysList(() => res.data)
				}
			})
			.catch((err) => {
				message.error(err)
				setLoading(false)
			})
	}
	/** 获取所有公司数据 */
	const onAllCompany = () => {
		getSupAndSubCompany().then((res: any) => {
			const list = translateDataToTree(res.data)
			setAllCompanyData(list as any)
		})
	}
	/** 获取所属公司数据 */
	const onCompany = () => {
		getCompany({merchantId: props.user.orgId}).then((res: any) => {
			const list = translateDataToTree(res.data)
			setCompanyData(list as any)
		})
	}

	// 根据主体id查询公司
	useEffect(() => {
		if (mainBodyId) {
			getCompany({merchantId: mainBodyId}).then((res: any) => {
				if (res?.success) {
					const data = res?.data
					res?.data?.forEach(
						(
							item: {disabled: boolean; type: any},
							index: number,
						) => {
							if (item.type !== 0) {
								data[index].disabled = true
							}
						},
					)
					const list = translateDataToTree(data)
					setCompanyData(list as any)
				}
			})
		}
	}, [mainBodyId])
	// 根据公司id查询城市
	useEffect(() => {
		if (orgId && (typeof orgId === 'number' || typeof orgId === 'string')) {
			if (props?.user?.permissionID) {
				getAllCitysPermission({
					permission: props?.user?.permissionID || '',
					orgId,
				})
					.then((res: any) => {
						if (res?.success) {
							setCitysList(() => res?.data)
						}
					})
					.catch((err) => {
						message.error(err)
						setLoading(false)
					})
			}
		}
	}, [orgId, props?.user])

	useEffect(() => {
		// 初始化字典数据
		getDict(
			{
				dictKeys: [
					'ZC_GYS',
					'JC_GYS',
					'CZ_GYS',
					'SP_GYS',
					'DYY_GYS',
					'CS_GYS',
					'Rating_Cycle',
					'Enterprise_Nature',
					'Invoicing_Tax_Rate',
					'Term_Payment',
					'Agency_Business',
					'Cooperation_Mode',
					'Business_Model',
					'Company_Type',
					'Channel_Type',
					'Settlement_Cycle',
					'Nature_Contract',
					'Contract_Status',
					'Business_Situation',
					'Category',
					'SP_Settlement_Method',
					'CS_Settlement_Method',
				],
			},
			(res: any) => {
				setDictData(res)
				// 组装供应商类型
				const newData = getSupplierType([
					res.ZC_GYS,
					res.JC_GYS,
					res.CZ_GYS,
					res.SP_GYS,
					res.DYY_GYS,
					res.CS_GYS,
				])
				setSupplierTypeData(newData)
			},
		)
		getSubjectFun()
		onAllCompany()
	}, [])
	useEffect(() => {
		if (supplierType?.[0] !== 'SP_GYS' && supplierType?.[0] !== 'CS_GYS') {
			getCitysFun()
			onCompany()
		} else {
			setCompanyData([])
			setCitysList([])
			admittanceForm.setFieldsValue({
				subject: undefined,
				orgId: undefined,
				city: undefined,
			})
		}
	}, [supplierType])

	// 根据路由判断当前页面状态
	useEffect(() => {
		const hrefs = window.location.href
		if (hrefs.includes('view')) {
			setType('view')
		} else if (hrefs.includes('add')) {
			setType('add')
		} else if (hrefs.includes('edit')) {
			setType('edit')
		} else {
			setType('process')
		}
	}, [])
	useEffect(() => {
		if (query?.id) {
			setLoading(true)
			viewSupplierAccess(query?.id)
				.then((res: any) => {
					if (res?.success) {
						setSupplierType(res.data?.supplierType?.split(','))
						const newSupplierType =
							res.data?.supplierType?.split(',')
						/** 组合准入建档数据 */
						const admittanceData = {
							...res.data,
							supplierType: res.data?.supplierType?.split(','),
							examineOrgId: res.data?.supplierBase?.examineOrgId,
							orgId:
								newSupplierType?.[0] === 'SP_GYS' ||
								newSupplierType?.[0] === 'CS_GYS'
									? res?.data?.orgId
									: res?.data?.orgId?.split(',').map(Number),
						}

						let urlDatas: any = {}
						if (newSupplierType?.[0] === 'SP_GYS') {
							urlDatas = res.data?.spInfo
							setMainBody(res.data?.subject)
							setOrgId(res.data?.orgId)
							admittanceData.city = [
								urlDatas?.provinceId,
								urlDatas?.cityId,
							]
						} else if (newSupplierType?.[0] === 'DYY_GYS') {
							urlDatas = res.data?.assetInfo
						} else if (
							newSupplierType?.[0] === 'CZ_GYS' &&
							newSupplierType?.[1] === '32'
						) {
							urlDatas = res.data?.handle
						} else {
							urlDatas = res.data?.supplierBase
							const data = getformDatas(res.data)
							basicsForm.setFieldsValue(data.basicsValue)
							const newBankData = res?.data?.bankData
							res?.data?.bankData?.forEach(
								(items: any, index: number) => {
									delete newBankData[index]?.id
								},
							)
							bankForm.setFieldsValue({bankData: newBankData})
						}
						if (newSupplierType?.[0] === 'CS_GYS') {
							setRequired(false)
							setMainBody(res.data?.subject)
							setOrgId(res.data?.orgId)
							admittanceData.city = [
								res.data?.supplierBase?.provinceId,
								res.data?.supplierBase?.cityId,
							]
						}
						admittanceForm.setFieldsValue(admittanceData)
						const url: any[] = getUrls(
							urlDatas,
							newSupplierType?.[0],
						)?.filter((item) => item !== 0)
						const newUrlData = getUrlData(
							urlDatas,
							newSupplierType?.[0],
						)
						setUrlData(newUrlData)
						if (url?.length) {
							getPublicDownloadUrl(url)
								.then((result: any) => {
									if (result?.success) {
										setEnclosureList(result?.data)
									}
								})
								.catch((err: any) => {
									message.error(err)
								})
						}
						setDataSource(res.data)
						setLoading(false)
					}
				})
				.catch((err) => {
					message.error(err)
					setLoading(false)
				})
		} else {
			getGenerateNumber('GYS').then((res: any) => {
				admittanceForm.setFieldsValue({
					supplierCode: res.data,
					supplierCreator: props?.user?.userName,
				})
			})
			// 初始一条银行信息
			bankForm.setFieldsValue({
				bankData: [
					{
						support: undefined,
						bankName: undefined,
						accountName: undefined,
						accountBankName: undefined,
						bankAccount: undefined,
					},
				],
			})
		}
	}, [query])

	const customizeHeader = (value: string) => (
		<div>
			<div className={styles.tip} />
			{value}
		</div>
	)

	const onSave = async (types: string) => {
		let newData: any = {}
		const admittanceValue = await admittanceForm.validateFields()
		const newAdmittanceValue = {
			...admittanceValue,
			orgId: admittanceValue?.orgId?.toString(),
			ratingCycle: admittanceValue?.ratingCycle || '',
		}
		let assetsData: any
		if (supplierType[0] === 'SP_GYS') {
			const channelData = await channelRef.current.getData()
			newData = {
				...newAdmittanceValue,
				...channelData,
				...urlData,
				provinceId: admittanceValue?.city?.[0] || '',
				cityId: admittanceValue?.city?.[1] || '',
			}
		} else if (supplierType[0] === 'DYY_GYS') {
			assetsData = await assetsRef.current.getData()
			newData = {...newAdmittanceValue, ...assetsData}
		} else if (supplierType[0] === 'CZ_GYS' && supplierType[1] === '32') {
			const managementData = await managementRef.current.getData()
			newData = {...newAdmittanceValue, ...managementData, ...urlData}
		} else {
			const basicsValue = await basicsForm.validateFields()
			const bankValue = await bankForm.validateFields()
			const newBankValue = bankValue?.bankData?.filter(
				(bankItem: any) =>
					bankItem?.support ||
					bankItem?.bankName ||
					bankItem?.bankAccount ||
					bankItem?.accountName ||
					bankItem?.accountBankName,
			)
			if (newAdmittanceValue && basicsValue && bankValue) {
				newData = {
					...newAdmittanceValue,
					...basicsValue,
					...urlData,
					enterpriseNature: basicsValue?.enterpriseNature || '',
					payCondition: basicsValue?.payCondition || '',
					invoicingRate: basicsValue?.invoicingRate || '',
					registeredCapital: basicsValue?.registeredCapital || 0,
					earnestMoney: basicsValue?.earnestMoney || 0,
					settlementMethod: basicsValue?.settlementMethod || '',
					bankData: newBankValue,
					establishDate: basicsValue?.establishDate
						? moment(basicsValue?.establishDate).format(
								'YYYY-MM-DD',
						  )
						: '1970-01-01',
					provinceId: basicsValue?.city?.[0] || '',
					cityId: basicsValue?.city?.[1] || '',
				}
			}
			if (supplierType[0] === 'CS_GYS') {
				newData.provinceId = admittanceValue?.city?.[0] || ''
				newData.cityId = admittanceValue?.city?.[1] || ''
			}
		}
		delete newData?.city
		setFormData({
			...newData,
			supplierType: newData.supplierType.toString(),
			fromType: 1,
			auditResult: types === 'save' ? 1 : 2,
		})
		if (supplierType[0] === 'DYY_GYS') {
			if (!assetsData?.businessLicenseUrl) {
				message.warning('请上传营业执照')
			} else {
				setIsModalOpen(true)
			}
		} else if (supplierType[0] === 'CS_GYS') {
			setIsModalOpen(true)
		} else {
			if (!urlData?.businessLicenseUrl) {
				message.warning('请上传营业执照')
			} else if (!urlData?.openingPermitUrl) {
				message.warning('请上传开户许可证')
			}
			if (urlData?.businessLicenseUrl && urlData?.openingPermitUrl) {
				setIsModalOpen(true)
			}
		}
	}
	const onSubmit = () => {
		let request: any
		setBtnLoading(true)
		if (query?.id) {
			// 编辑
			if (supplierType[0] === 'SP_GYS') {
				// sp供应商准入
				request = editSPSupplierAccess({...formData, id: query.id})
			} else if (supplierType[0] === 'DYY_GYS') {
				// 代运营资产方准入
				request = editAssetSupplierAccess({...formData, id: query.id})
			} else if (
				supplierType[0] === 'CZ_GYS' &&
				supplierType[1] === '32'
			) {
				// 处置供应商出售类准入
				request = editHandleSupplierAccess({...formData, id: query.id})
			} else {
				// 供应商准入
				request = editSupplierAccess({...formData, id: query.id})
			}
		} else if (supplierType[0] === 'SP_GYS') {
			// SP供应商准入
			request = addSPSupplierAccess(formData)
		} else if (supplierType[0] === 'DYY_GYS') {
			// 代运营资产方准入
			request = addAssetSupplierAccess(formData)
		} else if (supplierType[0] === 'CZ_GYS' && supplierType[1] === '32') {
			// 处置供应商出售类准入
			request = addHandleSupplierAccess(formData)
		} else {
			// 供应商准入
			request = addSupplierAccess(formData)
		}
		request
			.then((res: any) => {
				if (res.success) {
					message.success(res.msg)
					history.goBack()
					setBtnLoading(false)
				} else {
					setBtnLoading(false)
				}
			})
			.catch((err: any) => {
				message.error(err)
				setBtnLoading(false)
			})
	}
	const submit = async () => {
		if (
			query?.id &&
			dataSource?.socialCreditCode === formData?.socialCreditCode &&
			dataSource?.supplierType === formData?.supplierType
		) {
			onSubmit()
		} else {
			checkAccessSocialRepeat({
				socialCreditCode: formData?.socialCreditCode,
				supplierType: formData?.supplierType,
			})
				.then((result: any) => {
					if (result?.data === true) {
						onSubmit()
					} else {
						message.warning(
							'相同类型下的供应商，统一社会信用代码不能重复',
						)
					}
				})
				.catch((err) => {
					message.error(err)
				})
		}
	}

	const types =
		supplierType[0] !== 'DYY_GYS' &&
		supplierType[0] !== 'SP_GYS' &&
		supplierType[0] !== 'CS_GYS' &&
		supplierType?.[1] !== '32'

	const editData = () => {
		const newData = {
			auditResult: examineType === '同意' ? 4 : 3,
			id: query.id,
		}
		editCheckStatus(newData)
			.then((ress: any) => {
				if (ress.success) {
					message.success(ress.msg)
					history.goBack()
					setBtnLoading(false)
				} else {
					setBtnLoading(false)
				}
			})
			.catch((err: any) => {
				message.error(err)
				setBtnLoading(false)
			})
	}
	/** 审核 */
	const onCompleteTask = async () => {
		setBtnLoading(true)
		const examineData = await examineRef.current.getData()
		const userId = props?.user?.userId
		let request
		if (examineType === '拒绝') {
			request = deleteProcessInstance({
				processId: query?.processInstanceId,
				reason: JSON.stringify({
					url: examineData?.url,
					message: examineData?.message,
					userId: userId.toString(),
				}),
			})
		} else {
			request = completeTask({
				reviewerId: userId.toString(),
				processInstanceId: query?.processInstanceId,
				pass: examineType === '同意' ? 1 : 2,
				...examineData,
			})
		}
		request
			.then((res: any) => {
				if (res?.success) {
					if (res?.data?.processStatus !== 1) {
						if (examineType === '拒绝') {
							processRecordSave({
								bizType: 6,
								bizNo: query?.id,
								processId: query?.processInstanceId,
							})
						}
						editData()
					} else {
						history.goBack()
						setBtnLoading(false)
					}
				} else {
					setBtnLoading(false)
				}
			})
			.catch((err) => {
				message.error(err)
				setBtnLoading(false)
			})
	}

	return (
		<Spin spinning={loading}>
			<div className={styles.admittanceAdd}>
				<div className={styles.content}>
					{(type === 'view' || type === 'process') &&
					query?.processInstanceId ? (
						<Examine
							ref={examineRef}
							examineType={query?.examine === 'true'}
							orgId={props?.user?.orgId}
							processInstanceId={query?.processInstanceId}
							bizType={6}
							bizNo={query?.id}
						/>
					) : (
						''
					)}
					<Collapse
						defaultActiveKey={['1', '2', '3', '4', '9']}
						ghost
						expandIconPosition="end"
						collapsible="header">
						<Panel
							header={customizeHeader('准入建档')}
							key="1"
							forceRender>
							<Form
								form={admittanceForm}
								name="basic"
								layout="horizontal"
								labelCol={{flex: '140px'}}
								labelAlign="right"
								labelWrap
								autoComplete="off"
								disabled={
									type === 'view' || type === 'process'
								}>
								<Form.Item
									label="招标ID"
									name="biddingId"
									hidden>
									<Input
										placeholder={
											type === 'view' ||
											type === 'process'
												? '-'
												: '请输入'
										}
									/>
								</Form.Item>
								<Row gutter={12}>
									<Col
										sm={{span: 24}}
										md={{span: 12}}
										lg={{span: 8}}>
										<Form.Item
											label="供应商类型"
											name="supplierType"
											rules={[{required}]}
											colon>
											<Cascader
												onChange={(value) => {
													setSupplierType(value)
													admittanceForm.setFieldsValue(
														{orgId: undefined},
													)
													if (value[0] === 'CS_GYS') {
														setRequired(false)
													} else {
														setRequired(true)
													}
												}}
												options={supplierTypeData}
												placeholder={
													type === 'view' ||
													type === 'process'
														? '-'
														: '请选择'
												}
											/>
										</Form.Item>
									</Col>
									<Col
										sm={{span: 24}}
										md={{span: 12}}
										lg={{span: 8}}>
										<Form.Item
											label="统一社会信用代码"
											name="socialCreditCode"
											rules={[
												{
													required: true,
													message:
														'请输入统一社会信用代码',
												},
												{
													pattern:
														/^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g,
													message:
														'请输入正确的统一社会信用代码',
												},
											]}>
											<Input
												placeholder={
													type === 'view' ||
													type === 'process'
														? '-'
														: '请输入'
												}
												onBlur={(e) => {
													const queryData: any = {
														socialCreditCode:
															e.target.value,
														supplierType:
															supplierType.toString(),
													}
													getCheckSocialRepeat(
														queryData,
													).then((res: any) => {
														if (
															res?.data
																?.biddingAnswer
														) {
															delete res.data
																.biddingAnswer
																.supplierCode
															const data =
																res.data
																	.biddingAnswer
															setDataSource(data)
															admittanceForm.setFieldsValue(
																{
																	...data,
																	supplierType:
																		data?.supplierType?.split(
																			',',
																		),
																	supplierCreator:
																		props
																			?.user
																			?.userName ||
																		'',
																	biddingId:
																		data?.biddingId,
																	orgId: [],
																},
															)
															basicsForm.setFieldsValue(
																{
																	...data,
																	establishDate:
																		data?.establishDate
																			? moment(
																					data?.establishDate,
																			  )
																			: '',
																	city: data?.cityId
																		? [
																				data?.provinceId,
																				data?.cityId,
																		  ]
																		: [],
																},
															)
														} else {
															admittanceForm.setFieldsValue(
																{
																	biddingId:
																		'',
																},
															)
														}
													})
												}}
											/>
										</Form.Item>
									</Col>
									<Col
										sm={{span: 24}}
										md={{span: 12}}
										lg={{span: 8}}>
										<Form.Item
											label="供应商名称"
											name="supplierName"
											rules={[
												{
													required: true,
													max: 20,
												},
											]}>
											<Input
												placeholder={
													type === 'view' ||
													type === 'process'
														? '-'
														: '请输入'
												}
											/>
										</Form.Item>
									</Col>
									{supplierType?.[0] === 'SP_GYS' ||
									supplierType?.[0] === 'CS_GYS' ? (
										''
									) : (
										<Col
											sm={{span: 24}}
											md={{span: 12}}
											lg={{span: 8}}>
											<Form.Item
												label="供应商简称"
												name="abbreviation"
												rules={[{max: 10}]}>
												<Input
													placeholder={
														type === 'view' ||
														type === 'process'
															? '-'
															: '请输入'
													}
												/>
											</Form.Item>
										</Col>
									)}

									<Col
										sm={{span: 24}}
										md={{span: 12}}
										lg={{span: 8}}>
										<Form.Item
											label="供应商编号"
											name="supplierCode"
											rules={[
												{
													required: true,
												},
											]}>
											<Input
												placeholder={
													type === 'view' ||
													type === 'process'
														? '-'
														: '请输入'
												}
												disabled
											/>
										</Form.Item>
									</Col>
									<Col
										sm={{span: 24}}
										md={{span: 12}}
										lg={{span: 8}}>
										<Form.Item
											label="创建人"
											name="supplierCreator"
											rules={[{required}]}>
											<Input
												placeholder={
													type === 'view' ||
													type === 'process'
														? '-'
														: '请输入'
												}
												disabled
											/>
										</Form.Item>
									</Col>
									{types ? (
										<Col
											sm={{span: 24}}
											md={{span: 12}}
											lg={{span: 8}}>
											<Form.Item
												label="审核采购组织"
												name="examineOrgId"
												rules={[{required}]}>
												<TreeSelect
													showSearch
													style={{width: '100%'}}
													dropdownStyle={{
														maxHeight: 400,
														overflow: 'auto',
													}}
													placeholder={
														type === 'view' ||
														type === 'process'
															? '-'
															: '请选择'
													}
													allowClear
													treeDefaultExpandAll
													fieldNames={fieldNames}
													treeNodeFilterProp="name"
													treeDataSimpleMode
													treeData={allCompanyData}
												/>
											</Form.Item>
										</Col>
									) : (
										''
									)}
									{supplierType?.[0] === 'SP_GYS' ||
									supplierType?.[0] === 'CS_GYS' ? (
										<Col
											sm={{span: 24}}
											md={{span: 12}}
											lg={{span: 8}}>
											<Form.Item
												label="主体"
												name="subject"
												rules={[{required: true}]}>
												<Select
													placeholder="请选择"
													fieldNames={{
														label: 'merchantName',
														value: 'id',
													}}
													onChange={(value) => {
														setMainBody(value)
														admittanceForm.setFieldsValue(
															{
																orgId: undefined,
																city: undefined,
															},
														)
													}}
													options={mainBodyData}
												/>
											</Form.Item>
										</Col>
									) : (
										''
									)}
									<Col
										sm={{span: 24}}
										md={{span: 12}}
										lg={{span: 8}}>
										<Form.Item
											label="所属公司"
											name="orgId"
											rules={[
												{
													required: true,
												},
											]}>
											<TreeSelect
												showSearch
												style={{width: '100%'}}
												dropdownStyle={{
													maxHeight: 400,
													overflow: 'auto',
												}}
												placeholder={
													type === 'view' ||
													type === 'process'
														? '-'
														: '请选择'
												}
												allowClear
												multiple={
													!(
														supplierType?.[0] ===
															'SP_GYS' ||
														supplierType?.[0] ===
															'CS_GYS'
													)
												}
												showCheckedStrategy="SHOW_ALL"
												treeDefaultExpandAll
												treeNodeFilterProp="name"
												fieldNames={{
													label: 'name',
													value: 'id',
												}}
												treeDataSimpleMode
												treeData={companyData}
												onChange={(value) => {
													setOrgId(value)
													admittanceForm.setFieldsValue(
														{
															city: undefined,
														},
													)
												}}
											/>
										</Form.Item>
									</Col>
									{supplierType?.[0] === 'SP_GYS' ||
									supplierType?.[0] === 'CS_GYS' ? (
										<Col
											sm={{span: 24}}
											md={{span: 12}}
											lg={{span: 8}}>
											<Form.Item
												label="城市"
												name="city"
												rules={[{required: true}]}>
												<Cascader
													fieldNames={cityNames}
													options={citysList}
													placeholder={
														type === 'view' ||
														type === 'process'
															? '-'
															: '请选择'
													}
												/>
											</Form.Item>
										</Col>
									) : (
										''
									)}
									<Col
										sm={{span: 24}}
										md={{span: 12}}
										lg={{span: 8}}>
										<Form.Item
											label="评定周期"
											name="ratingCycle"
											rules={[
												{
													required:
														supplierType?.[0] ===
															'SP_GYS' ||
														supplierType?.[0] ===
															'CS_GYS'
															? false
															: required,
												},
											]}>
											<Select
												placeholder={
													type === 'view' ||
													type === 'process'
														? '-'
														: '请选择'
												}
												allowClear
												fieldNames={{
													value: 'dictKey',
													label: 'dictValue',
												}}
												options={dictData?.Rating_Cycle}
											/>
										</Form.Item>
									</Col>
									<Col
										span={16}
										style={{marginRight: '10px'}}>
										<Form.Item
											label="引入原因"
											name="reason">
											<TextArea
												maxLength={200}
												placeholder={
													type === 'view' ||
													type === 'process'
														? '-'
														: '请输入（最多200个字）'
												}
												autoSize={{
													minRows: 3,
													maxRows: 6,
												}}
											/>
										</Form.Item>
									</Col>
								</Row>
							</Form>
						</Panel>
						{supplierType[0] === 'DYY_GYS' ||
						supplierType[0] === 'SP_GYS' ||
						(supplierType[0] === 'CZ_GYS' &&
							supplierType?.[1] === '32') ? (
							''
						) : (
							<>
								<Panel
									header={customizeHeader('基础信息')}
									key="2"
									forceRender>
									<Form
										form={basicsForm}
										name="basic"
										layout="horizontal"
										labelCol={{flex: '140px'}}
										labelAlign="right"
										labelWrap
										autoComplete="off"
										disabled={
											type === 'view' ||
											type === 'process'
										}>
										<Row gutter={12}>
											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="企业性质"
													name="enterpriseNature"
													rules={[{required}]}>
													<Select
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请选择'
														}
														allowClear
														fieldNames={{
															value: 'dictKey',
															label: 'dictValue',
														}}
														options={
															dictData?.Enterprise_Nature
														}
													/>
												</Form.Item>
											</Col>
											{supplierType?.[0] === 'CS_GYS' ? (
												''
											) : (
												<Col
													sm={{span: 24}}
													md={{span: 12}}
													lg={{span: 8}}>
													<Form.Item
														label="注册城市"
														name="city"
														rules={[{required}]}>
														<Cascader
															fieldNames={
																cityNames
															}
															options={citysList}
															placeholder={
																type ===
																	'view' ||
																type ===
																	'process'
																	? '-'
																	: '请选择'
															}
														/>
													</Form.Item>
												</Col>
											)}

											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="常用地址"
													name="address"
													rules={[
														{
															required:
																supplierType?.[0] ===
																'CS_GYS'
																	? false
																	: required,
														},
														{max: 30},
													]}>
													<Input
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请输入'
														}
													/>
												</Form.Item>
											</Col>
											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="付款条件"
													name="payCondition">
													<Select
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请选择'
														}
														allowClear
														fieldNames={{
															value: 'dictKey',
															label: 'dictValue',
														}}
														options={
															dictData?.Term_Payment
														}
													/>
												</Form.Item>
											</Col>
											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="开票税率"
													name="invoicingRate"
													rules={[{required}]}>
													<Select
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请选择'
														}
														allowClear
														fieldNames={{
															value: 'dictKey',
															label: 'dictValue',
														}}
														options={
															dictData?.Invoicing_Tax_Rate
														}
													/>
												</Form.Item>
											</Col>
											{supplierType?.[0] === 'CS_GYS' ? (
												''
											) : (
												<Col
													sm={{span: 24}}
													md={{span: 12}}
													lg={{span: 8}}>
													<Form.Item
														label="固定电话"
														name="telephone"
														rules={[
															{
																pattern:
																	/^(((\d{3,4})?[0-9]{7,8})|(1(3|4|5|6|7|8|9)\d{9}))$/,
																message:
																	'填写的电话格式有误',
															},
														]}>
														<Input
															placeholder={
																type ===
																	'view' ||
																type ===
																	'process'
																	? '-'
																	: '请输入'
															}
														/>
													</Form.Item>
												</Col>
											)}

											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="联系人"
													name="contacts"
													rules={[
														{required, max: 6},
													]}>
													<Input
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请输入'
														}
													/>
												</Form.Item>
											</Col>
											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="联系人手机"
													name="mobilePhone"
													rules={[
														{
															pattern:
																/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
															message:
																'请输入正确的手机号',
														},
													]}>
													<Input
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请输入'
														}
													/>
												</Form.Item>
											</Col>
											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="联系人邮箱"
													name="email"
													rules={[
														{
															type: 'email',
															message:
																'填写的邮箱格式有误',
														},
													]}>
													<Input
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请输入'
														}
													/>
												</Form.Item>
											</Col>
											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="注册资本"
													name="registeredCapital"
													rules={[
														{required},
														{
															pattern:
																/^([0-9]|[1-9]\d+)(\.\d{0,2})?$/,
															message:
																'小数点后不得超过两位',
														},
													]}>
													<InputNumber
														formatter={(value) => {
															if (
																type ===
																	'view' ||
																type ===
																	'process'
															) {
																return `${value}元`
															}
															return (
																value?.toString() ||
																''
															)
														}}
														max={1000000000}
														addonAfter="元"
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请输入'
														}
													/>
												</Form.Item>
											</Col>
											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="成立日期"
													name="establishDate"
													rules={[{required}]}>
													<DatePicker
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请选择'
														}
													/>
												</Form.Item>
											</Col>
											<Col
												sm={{span: 24}}
												md={{span: 12}}
												lg={{span: 8}}>
												<Form.Item
													label="邮编"
													name="postalCode"
													rules={[
														{
															pattern:
																/^[0-9]\d{5}$/,
															message:
																'请输入正确的邮编',
														},
													]}>
													<Input
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请输入'
														}
													/>
												</Form.Item>
											</Col>
											{supplierType[0] === 'CS_GYS' ? (
												<>
													{' '}
													<Col
														sm={{span: 24}}
														md={{span: 12}}
														lg={{span: 8}}>
														<Form.Item
															label="结算方式"
															name="settlementMethod">
															<Select
																placeholder={
																	type ===
																		'view' ||
																	type ===
																		'process'
																		? '-'
																		: '请选择'
																}
																allowClear
																fieldNames={{
																	value: 'dictKey',
																	label: 'dictValue',
																}}
																options={
																	dictData?.CS_Settlement_Method
																}
															/>
														</Form.Item>
													</Col>
													<Col
														sm={{span: 24}}
														md={{span: 12}}
														lg={{span: 8}}>
														<Form.Item
															label="保证金金额"
															name="earnestMoney"
															rules={[
																{
																	pattern:
																		/^([0-9]|[1-9]\d+)(\.\d{0,2})?$/,
																	message:
																		'小数点后不得超过两位',
																},
															]}>
															<InputNumber
																formatter={(
																	value,
																) => {
																	if (
																		type ===
																			'view' ||
																		type ===
																			'process'
																	) {
																		return `${value}元`
																	}
																	return (
																		value?.toString() ||
																		''
																	)
																}}
																max={1000000000}
																addonAfter="元"
																placeholder={
																	type ===
																		'view' ||
																	type ===
																		'process'
																		? '-'
																		: '请输入'
																}
															/>
														</Form.Item>
													</Col>
												</>
											) : (
												''
											)}

											<Col span={16}>
												<Form.Item
													label="公司简介"
													name="companyIntroduction">
													<TextArea
														maxLength={200}
														placeholder={
															type === 'view' ||
															type === 'process'
																? '-'
																: '请输入（最多200个字）'
														}
														autoSize={{
															minRows: 3,
															maxRows: 6,
														}}
													/>
												</Form.Item>
											</Col>
										</Row>
									</Form>
								</Panel>
								<Panel
									header={customizeHeader('银行信息')}
									key="3"
									forceRender>
									<Form
										form={bankForm}
										name="basic"
										layout="horizontal"
										labelCol={{flex: '140px'}}
										labelAlign="right"
										labelWrap
										disabled={
											type === 'view' ||
											type === 'process'
										}
										autoComplete="off">
										<Form.List name="bankData">
											{(fields, {add, remove}) => (
												<>
													{fields.map(
														(field, index) => (
															<Row
																gutter={12}
																key={field.key}>
																<Col
																	sm={{
																		span: 24,
																	}}
																	md={{
																		span: 12,
																	}}
																	lg={{
																		span: 8,
																	}}>
																	<Form.Item
																		label="支持银承"
																		name={[
																			field.name,
																			'support',
																		]}
																		rules={[
																			{
																				required,
																			},
																		]}>
																		<Select
																			placeholder={
																				type ===
																					'view' ||
																				type ===
																					'process'
																					? '-'
																					: '请选择'
																			}
																			allowClear>
																			<Option
																				value={
																					1
																				}>
																				是
																			</Option>
																			<Option
																				value={
																					2
																				}>
																				否
																			</Option>
																		</Select>
																	</Form.Item>
																</Col>
																<Col
																	sm={{
																		span: 24,
																	}}
																	md={{
																		span: 12,
																	}}
																	lg={{
																		span: 8,
																	}}>
																	<Form.Item
																		label="开户银行"
																		name={[
																			field.name,
																			'bankName',
																		]}
																		rules={[
																			{
																				required,
																				max: 20,
																			},
																			{
																				pattern:
																					/^[\u4e00-\u9fa5]{0,}$/,
																				message:
																					'开户银行只能输入文字',
																			},
																		]}>
																		<Input
																			placeholder={
																				type ===
																					'view' ||
																				type ===
																					'process'
																					? '-'
																					: '请输入'
																			}
																		/>
																	</Form.Item>
																</Col>
																<Col
																	sm={{
																		span: 24,
																	}}
																	md={{
																		span: 12,
																	}}
																	lg={{
																		span: 8,
																	}}>
																	<Form.Item
																		label="开户名称"
																		name={[
																			field.name,
																			'accountName',
																		]}
																		rules={[
																			{
																				required,
																				max: 20,
																			},
																			{
																				pattern:
																					/^[\u4e00-\u9fa5]{0,}$/,
																				message:
																					'开户名称只能输入文字',
																			},
																		]}>
																		<Input
																			placeholder={
																				type ===
																					'view' ||
																				type ===
																					'process'
																					? '-'
																					: '请输入'
																			}
																		/>
																	</Form.Item>
																</Col>
																<Col
																	sm={{
																		span: 24,
																	}}
																	md={{
																		span: 12,
																	}}
																	lg={{
																		span: 8,
																	}}>
																	<Form.Item
																		label="开户账号"
																		name={[
																			field.name,
																			'bankAccount',
																		]}
																		rules={[
																			{
																				required,
																				max: 20,
																			},
																			{
																				pattern:
																					/^[0-9]*$/,
																				message:
																					'开户账号只能输入纯数字',
																			},
																		]}>
																		<Input
																			placeholder={
																				type ===
																					'view' ||
																				type ===
																					'process'
																					? '-'
																					: '请输入'
																			}
																		/>
																	</Form.Item>
																</Col>
																<Col
																	sm={{
																		span: 24,
																	}}
																	md={{
																		span: 12,
																	}}
																	lg={{
																		span: 8,
																	}}>
																	<Form.Item
																		label="开户行名称"
																		name={[
																			field.name,
																			'accountBankName',
																		]}
																		rules={[
																			{
																				required,
																				max: 20,
																			},
																			{
																				pattern:
																					/^[\u4e00-\u9fa5]{0,}$/,
																				message:
																					'开户行名称只能输入文字',
																			},
																		]}>
																		<Input
																			placeholder={
																				type ===
																					'view' ||
																				type ===
																					'process'
																					? '-'
																					: '请输入'
																			}
																		/>
																	</Form.Item>
																</Col>
																{index === 0 ||
																type ===
																	'view' ||
																type ===
																	'process' ? (
																	''
																) : (
																	<Col
																		sm={{
																			span: 24,
																		}}
																		md={{
																			span: 12,
																		}}
																		lg={{
																			span: 8,
																		}}>
																		<MinusCircleOutlined
																			className={
																				styles.remove
																			}
																			onClick={() =>
																				remove(
																					index,
																				)
																			}
																		/>
																	</Col>
																)}
															</Row>
														),
													)}
													{fields?.length < 5 &&
													type !== 'view' &&
													type !== 'process' ? (
														<Form.Item>
															<Button
																type="link"
																icon={
																	<PlusCircleOutlined />
																}
																onClick={() =>
																	add()
																}>
																添加账号信息
															</Button>
														</Form.Item>
													) : (
														''
													)}
												</>
											)}
										</Form.List>
									</Form>
								</Panel>
							</>
						)}
						{supplierType[0] === 'SP_GYS' ? (
							<ChannelSupplier
								ref={channelRef}
								type={type}
								dictData={dictData}
								citysList={citysList}
								dataSource={dataSource}
								enclosureList={
									enclosureList?.length &&
									enclosureList?.filter(
										(item) => item?.targetAttr === 13,
									)
								}
							/>
						) : (
							''
						)}
						{supplierType[0] === 'CZ_GYS' &&
						supplierType?.[1] === '32' ? (
							<ManagementSupplier
								ref={managementRef}
								type={type}
								dictData={dictData}
								citysList={citysList}
								dataSource={dataSource}
							/>
						) : (
							''
						)}

						{supplierType[0] === 'DYY_GYS' ? (
							<Assets
								ref={assetsRef}
								type={type}
								dictData={dictData}
								dataSource={dataSource}
								enclosureList={enclosureList}
							/>
						) : (
							<Panel
								header={customizeHeader('资质信息')}
								key="4"
								forceRender>
								{enclosureList && (
									<Qualifications
										required={required}
										urlData={urlData}
										setUrlData={setUrlData}
										enclosureList={enclosureList}
										type={type}
										supplierType={supplierType[0]}
									/>
								)}
							</Panel>
						)}
						{type === 'view' ? (
							<Panel
								header={customizeHeader('操作记录')}
								key="9"
								forceRender>
								<OperationRecord
									businessId={query?.id}
									businessType={8}
								/>
							</Panel>
						) : (
							''
						)}
					</Collapse>
					<div className={styles.wrap}>
						{type === 'process' && query?.examine === 'true' ? (
							<>
								<Button
									type="primary"
									htmlType="submit"
									onClick={() => {
										setExamineType('同意')
										setIsExamineModalOpen(true)
									}}>
									同意
								</Button>
								<Button
									onClick={() => {
										setExamineType('驳回')
										setIsExamineModalOpen(true)
									}}>
									驳回
								</Button>
								<Button
									onClick={() => {
										setExamineType('拒绝')
										setIsExamineModalOpen(true)
									}}>
									拒绝
								</Button>
							</>
						) : (
							''
						)}
						{type !== 'view' && type !== 'process' ? (
							<>
								<Button
									type="primary"
									htmlType="submit"
									loading={btnLoading}
									onClick={() => {
										onSave('save')
										setSaveType(true)
									}}>
									保存
								</Button>
								<Button
									type="primary"
									htmlType="submit"
									loading={btnLoading}
									onClick={() => {
										onSave('submit')
										setSaveType(false)
									}}>
									提交
								</Button>
								<Button
									onClick={() => {
										history.go(-1)
									}}>
									取消
								</Button>
							</>
						) : (
							<Button
								onClick={() => {
									history.go(-1)
								}}>
								返回
							</Button>
						)}
					</div>
				</div>
				<Modal
					text={
						saveType
							? `确定要保存吗？请仔细核对所填信息`
							: '确定要提交吗？提交后自动保存并开始审批，请仔细核对所填信息'
					}
					onSubmit={submit}
					loading={btnLoading}
					isModalOpen={isModalOpen}
					setIsModalOpen={setIsModalOpen}
				/>
				<Modal
					text={`确定要${examineType}吗？请仔细核对所填信息`}
					loading={btnLoading}
					onSubmit={onCompleteTask}
					isModalOpen={isExamineModalOpen}
					setIsModalOpen={setIsExamineModalOpen}
				/>
			</div>
		</Spin>
	)
}
export default connect(({user}: any) => ({user}))(newlyAdded)
